package com.ai.wx.main;

import java.net.URLEncoder;

import com.ai.core.util.Configure;
import com.ai.wx.pojo.AccessToken;
import com.ai.wx.pojo.Button;
import com.ai.wx.pojo.CommonButton;
import com.ai.wx.pojo.ComplexButton;
import com.ai.wx.pojo.Menu;
import com.ai.wx.pojo.ViewButton;
import com.ai.wx.util.WeixinUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 菜单管理器类
 * 
 * @author liufeng
 * @date 2013-08-08
 */
public class MenuManager {
	private static Logger log = LoggerFactory.getLogger(MenuManager.class);

	public static void main(String[] args) {
		// 第三方用户唯一凭证
		String appId = Configure.getAppId();
		// 第三方用户唯一凭证密钥
		String appSecret = Configure.getAppsecret();

		// 调用接口获取access_token
		AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);

		if (null != at) {
			// 调用接口创建菜单
			int result = WeixinUtil.createMenu(getMenu(appId), at.getToken());

			// 判断菜单创建结果
			if (0 == result)
				log.info("菜单创建成功！");
			else
				log.info("菜单创建失败，错误码：" + result);
		}
	}

	/**
	 * 组装菜单数据
	 * 
	 * @return
	 */
	private static Menu getMenu(String appId) {

		ComplexButton btn10 = new ComplexButton();
		btn10.setName("订购流量包");
		
		ViewButton btn11 = new ViewButton();
		btn11.setName("订购流量包");
		btn11.setType("view");
		btn11.setUrl(getMenuUrl(appId,"http://www.flow-market.cn/wx/buy"));
		ViewButton btn12 = new ViewButton();
		
		btn12.setName("订单查询");
		btn12.setType("view");
		btn12.setUrl(getMenuUrl(appId,"http://www.flow-market.cn/wx/order"));

		btn10.setSub_button(new Button[] { btn11,  btn12}); 
		
		ComplexButton btn20 = new ComplexButton();
		btn20.setName("推广");
		
		ViewButton btn21 = new ViewButton();
		btn21.setName("推广资格申请");
		btn21.setType("view"); 
		btn21.setUrl(getMenuUrl(appId,"http://www.flow-market.cn/wx/agent/agentUser"));

		ViewButton btn23 = new ViewButton();
		btn23.setName("优惠码设置");
		btn23.setType("view"); 
		btn23.setUrl(getMenuUrl(appId,"http://www.flow-market.cn/wx/agent/promotionCodeSetting"));
		
		ViewButton btn22 = new ViewButton();
		btn22.setName("我的推广金");
		btn22.setType("view"); 
		btn22.setUrl(getMenuUrl(appId,"http://www.flow-market.cn/wx/agent/brokerage"));
 
		btn20.setSub_button(new Button[] { btn21,btn23, btn22});  
		
		ViewButton btn30 = new ViewButton();
		btn30.setName("流量查询");
		btn30.setType("view");
		btn30.setUrl(getMenuUrl(appId,"http://www.flow-market.cn/wx/query")); 
		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		Menu menu = new Menu();
		menu.setButton(new Button[] {btn10,btn30, btn20 });

		return menu;
	}
	
	private static String getMenuUrl(String appId,String url){
		String wxUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
		
		String menuUrl = wxUrl.replaceAll("APPID", appId).replaceAll("REDIRECT_URI", URLEncoder.encode(url)).replaceAll("STATE", "123").replaceAll("SCOPE", "snsapi_base");
		return menuUrl;
	}
}